package org.linlinjava.litemall.admin.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.constant.DBConmonConstant;
import org.linlinjava.litemall.db.domain.LitemallAdmin;
import org.linlinjava.litemall.db.domain.LitemallFormPurchase;
import org.linlinjava.litemall.db.domain.LitemallFormPurchaseExample;
import org.linlinjava.litemall.db.domain.LitemallShop;
import org.linlinjava.litemall.db.service.LitemallHerbService;
import org.linlinjava.litemall.db.service.LitemallPurchaseAuditService;
import org.linlinjava.litemall.db.service.LitemallPurchaseService;
import org.linlinjava.litemall.db.service.LitemallShopService;
import org.linlinjava.litemall.db.service.LitemallUserAuditService;
import org.linlinjava.litemall.db.service.LitemallUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AdminDashbordService {
	@Autowired
	private LitemallShopService shopService;
	@Autowired
	private LitemallUserService userService;
	@Autowired
	private LitemallHerbService herbService;
	@Autowired
	private LitemallUserAuditService userAuditService;
	@Autowired
	private LitemallPurchaseAuditService purchaseAuditService;
	@Autowired
	private LitemallPurchaseService purchaseService;

	public Object info() {
		Subject currentUser = SecurityUtils.getSubject();
		LitemallAdmin admin = (LitemallAdmin) currentUser.getPrincipal();
		LitemallShop shop = null;
		if (admin.getShopId() != null) {
			shop = shopService.findById(admin.getShopId());
		}

		Map<String, Object> data = new HashMap<>();
		String identity = "admin";

		if (shop != null && DBConmonConstant.UserType.Manufactor.getValue() == shop.getUserType()) {
			// 饮片厂登录
			identity = "manufactor";
			data.put(identity, factoryInfo(shop));
		} else if (shop != null && DBConmonConstant.UserType.Distributor.getValue() == shop.getUserType()) {
			// 经销商登录
			identity = "distributor";
			data.put(identity, distributorInfo(shop));
		} else {
			// 平台管理员登录
			data.put(identity, adminInfo());
		}

		data.put("identity", identity);
		return ResponseUtil.ok(data);
	}

	/**
	 * 平台管理员
	 * 
	 * @return
	 */
	private Map<String, Object> adminInfo() {
		int userTotal = userService.count();
		int herbTotal = herbService.count();
		int userAuditTotal = userAuditService.count();
		int purchaseAuditTotal = purchaseAuditService.count();

		Map<String, Object> data = new HashMap<>();

		data.put("userTotal", userTotal);
		data.put("herbTotal", herbTotal);
		data.put("userAuditTotal", userAuditTotal);
		data.put("purchaseAuditTotal", purchaseAuditTotal);

		return data;
	}

	/**
	 * 饮片厂
	 * 
	 * @return
	 */
	private Map<String, Object> factoryInfo(LitemallShop shop) {
		LitemallFormPurchaseExample example = new LitemallFormPurchaseExample();
		LitemallFormPurchaseExample.Criteria criteria = example.createCriteria();
		criteria.andShopIdEqualTo(shop.getId());
		criteria.andDeletedEqualTo(false);
		List<LitemallFormPurchase> purchaseList = purchaseService.selectByExample(example);

		int waitReleased = 0;// 待发布
		int underReview = 0;// 审核中
		int reviewed = 0;// 已审核
		int closed = 0;// 已完结
		if (CollectionUtils.isNotEmpty(purchaseList)) {
			for (LitemallFormPurchase purchase : purchaseList) {
				if (checkEqual((byte) 0, purchase.getStatus())) {
					waitReleased++;
				}
				if (checkEqual((byte) 1, purchase.getStatus())) {
					underReview++;
				}
				if (checkEqual((byte) 2, purchase.getStatus())) {
					reviewed++;
				}
				if (checkEqual((byte) 4, purchase.getStatus())) {
					closed++;
				}
			}
		}

		Map<String, Object> data = new HashMap<>();
		data.put("waitReleased", waitReleased);
		data.put("underReview", underReview);
		data.put("reviewed", reviewed);
		data.put("closed", closed);
		return data;
	}

	/**
	 * 经销商、代理商
	 * 
	 * @return
	 */
	private Map<String, Object> distributorInfo(LitemallShop shop) {
		LitemallFormPurchaseExample example = new LitemallFormPurchaseExample();
		LitemallFormPurchaseExample.Criteria criteria = example.createCriteria();
		criteria.andShopIdEqualTo(shop.getId());
		criteria.andDeletedEqualTo(false);
		List<LitemallFormPurchase> purchaseList = purchaseService.selectByExample(example);

		int waitReleased = 0;// 待发布
		int underReview = 0;// 审核中
		int reviewed = 0;// 已审核
		int closed = 0;// 已完结
		if (CollectionUtils.isNotEmpty(purchaseList)) {
			for (LitemallFormPurchase purchase : purchaseList) {
				if (checkEqual((byte) 0, purchase.getStatus())) {
					waitReleased++;
				}
				if (checkEqual((byte) 1, purchase.getStatus())) {
					underReview++;
				}
				if (checkEqual((byte) 2, purchase.getStatus())) {
					reviewed++;
				}
				if (checkEqual((byte) 4, purchase.getStatus())) {
					closed++;
				}
			}
		}

		Map<String, Object> data = new HashMap<>();
		data.put("waitReleased", waitReleased);
		data.put("underReview", underReview);
		data.put("reviewed", reviewed);
		data.put("closed", closed);
		return data;
	}

	public static boolean checkEqual(Byte v1, Byte v2) {
		if (v1 == null || v2 == null) {
			return false;
		}
		return v1.byteValue() == v2.byteValue();
	}
}
